package com.u.social.search.service;

import com.u.social.search.model.ArticleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: u-social
 * @description: 搜索逻辑类
 * @author: Alex Wu
 * @createDate: 2025-04-11 10:27
 **/

@Service
public class SearchService {

    private final String associationalDueDate = "一个月以内";


    @Autowired
    private AIApiService aiApiService;

    @Autowired
    private RecallService recallService;

    @Autowired
    private RoughRankingService roughRankingService;

    @Autowired
    private FineRankingService fineRankingService;

    @Autowired
    private ReRankingService reRankingService;



    public List<ArticleDTO> search(String keyword) {
        // 1-1. 关键词合法性校验
        String validateKeyword = validateKeyword(keyword);
        if (validateKeyword == null) {
            System.out.println("关键词不合法");
            return null;
        }
        // 1-2 调用AI远程能力查询语义理解
        String realIntent = aiApiService.getRealIntent(validateKeyword);

        // 1-3. 调用同义词库查询同义词
        String synonymParticiple = getSynonymParticiple(realIntent);

        // 2-1. 召回
        List<ArticleDTO> recallByTextRelated = recallService.getRecallByTextRelated(synonymParticiple);
        // 2-2. 召回-embedding
        List<ArticleDTO> recallByEmbedding = recallService.getRecallByEmbedding(synonymParticiple);
        // 3-1. 粗排召回结果去重
        List<ArticleDTO> result = new ArrayList<>();
        for (ArticleDTO articleDTO : recallByTextRelated) {
            if (!result.contains(articleDTO)) {
                result.add(articleDTO);
            }
        }
        for (ArticleDTO articleDTO : recallByEmbedding) {
            if (!result.contains(articleDTO)) {
                result.add(articleDTO);
            }
        }
        // 3-2 粗排-根据文本相关性，质量分，转化率加权权重打分 2000 里面取200
        List<ArticleDTO> roughSortedList = roughRankingService.roughSortByWeightRule(result);
        List<Long> roughSortedIds = roughSortedList.stream()
                .map(ArticleDTO::getId)
                .toList();

        // 4. 精排
        List<ArticleDTO> articleDTOS = fineRankingService.elaborateSort(roughSortedIds, 12345L);

        // 5. 重排
        List<ArticleDTO> reRankArticles = reRankingService.reRankByType(articleDTOS);

        return reRankArticles;
    }



    private String getSynonymParticiple(String keyword) {
        System.out.println("加载同义词库");
        System.out.println("jieba 分词");
        System.out.println("分词结果：" + keyword);
        return keyword;

    }



    private String validateKeyword(String keyword) {
        System.out.println("验证关键词是否合法");
        System.out.println("关键词是否在黑名单里面");
        System.out.println("文本纠错识别");
        return keyword;
    }

    public List<ArticleDTO> searchAssociationalWord(String keyword) {
        // 查询一个月内的联想词并根据GTV排序
        /** ESClient esClient = new ESClient();
         esClient.searchAssociationalWord(keyword, associationalDueDate, orderByHot); **/

        System.out.println("用ES查询一个月内的联想词并根据GTV排序");
        System.out.println("搜索联想词：" + keyword);
        return new ArrayList<ArticleDTO>();
    }

    /**
     * 查询热门搜索词
     *
     * @return 热门搜索词列表
     */
    public List<String> searchHotWord() {
        System.out.println("从Redis查询热门搜索词");
        return new ArrayList<String>();
    }
}
